{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Xo4cz5r0c4yy"
      },
      "outputs": [],
      "source": [
        "# Copyright 2021 Google LLC\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#     https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_title:migration,automl,icn"
      },
      "source": [
        "# AutoML text sentiment analysis\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JE-aKjayJjkF"
      },
      "source": [
        "## Installation\n",
        "\n",
        "Install the latest version of AutoML SDK."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7dxB6-B0JjkG"
      },
      "outputs": [],
      "source": [
        "! pip3 install google-cloud-automl"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Iddo-8HmJjkH"
      },
      "source": [
        "Install the Google *cloud-storage* library as well."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xHvi6EFSJjkH"
      },
      "outputs": [],
      "source": [
        "! pip3 install google-cloud-storage"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ktRTgB8DJjkI"
      },
      "source": [
        "### Restart the Kernel\n",
        "\n",
        "Once you've installed the AutoML SDK and Google *cloud-storage*, you need to restart the notebook kernel so it can find the packages."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "x1g4mBdlJjkI"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "if not os.getenv(\"AUTORUN\"):\n",
        "    # Automatically restart kernel after installs\n",
        "    import IPython\n",
        "\n",
        "    app = IPython.Application.instance()\n",
        "    app.kernel.do_shutdown(True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AGX8yszodHx_"
      },
      "source": [
        "## Before you begin\r\n",
        "\r\n",
        "### GPU run-time\r\n",
        "\r\n",
        "*Make sure you're running this notebook in a GPU runtime if you have that option. In Colab, select* **Runtime > Change Runtime Type > GPU**\r\n",
        "\r\n",
        "### Set up your GCP project\r\n",
        "\r\n",
        "**The following steps are required, regardless of your notebook environment.**\r\n",
        "\r\n",
        "1. [Select or create a GCP project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\r\n",
        "\r\n",
        "2. [Make sure that billing is enabled for your project.](https://cloud.google.com/billing/docs/how-to/modify-project)\r\n",
        "\r\n",
        "3. [Enable the AutoML APIs and Compute Engine APIs.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component)\r\n",
        "\r\n",
        "4. [Google Cloud SDK](https://cloud.google.com/sdk) is already installed in AutoML Notebooks.\r\n",
        "\r\n",
        "5. Enter your project ID in the cell below. Then run the  cell to make sure the\r\n",
        "Cloud SDK uses the right project for all the commands in this notebook.\r\n",
        "\r\n",
        "**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands.\r\n",
        "\r\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "set_project_id"
      },
      "outputs": [],
      "source": [
        "PROJECT_ID = \"[your-project-id]\"  # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "autoset_project_id"
      },
      "outputs": [],
      "source": [
        "if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
        "    # Get your GCP project id from gcloud\n",
        "    shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
        "    PROJECT_ID = shell_output[0]\n",
        "    print(\"Project ID:\", PROJECT_ID)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "set_gcloud_project_id"
      },
      "outputs": [],
      "source": [
        "! gcloud config set project $PROJECT_ID"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jT0fijejJjkK"
      },
      "source": [
        "#### Region\n",
        "\n",
        "You can also change the `REGION` variable, which is used for operations\n",
        "throughout the rest of this notebook.  Below are regions supported for AutoML. We recommend when possible, to choose the region closest to you.\n",
        "\n",
        "- Americas: `us-central1`\n",
        "- Europe: `europe-west4`\n",
        "- Asia Pacific: `asia-east1`\n",
        "\n",
        "You cannot use a Multi-Regional Storage bucket for training with AutoML. Not all regions provide support for all AutoML services. For the latest support per region, see [Region support for AutoML services]()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "CTBlncfrJjkK"
      },
      "outputs": [],
      "source": [
        "REGION = \"us-central1\"  # @param {type: \"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "J_croPBoJjkK"
      },
      "source": [
        "#### Timestamp\n",
        "\n",
        "If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append onto the name of resources which will be created in this tutorial."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KKxStk5bJjkL"
      },
      "outputs": [],
      "source": [
        "from datetime import datetime\n",
        "\n",
        "TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c3yKEDsvdpFf"
      },
      "source": [
        "### Authenticate your GCP account\r\n",
        "\r\n",
        "**If you are using AutoML Notebooks**, your environment is already\r\n",
        "authenticated. Skip this step.\r\n",
        "\r\n",
        "*Note: If you are on an AutoML notebook and run the cell, the cell knows to skip executing the authentication steps.*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "chybg3Ap_i_2"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import sys\n",
        "\n",
        "# If you are running this notebook in Colab, run this cell and follow the\n",
        "# instructions to authenticate your Google Cloud account. This provides access\n",
        "# to your Cloud Storage bucket and lets you submit training jobs and prediction\n",
        "# requests.\n",
        "\n",
        "# If on Vertex, then don't execute this code\n",
        "if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
        "    if \"google.colab\" in sys.modules:\n",
        "        from google.colab import auth as google_auth\n",
        "\n",
        "        google_auth.authenticate_user()\n",
        "\n",
        "    # If you are running this tutorial in a notebook locally, replace the string\n",
        "    # below with the path to your service account key and run this cell to\n",
        "    # authenticate your Google Cloud account.\n",
        "    else:\n",
        "        %env GOOGLE_APPLICATION_CREDENTIALS your_path_to_credentials.json\n",
        "\n",
        "    # Log in to your account on Google Cloud\n",
        "    ! gcloud auth login"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MDUAZaN3JjkL"
      },
      "source": [
        "### Create a Cloud Storage bucket\n",
        "\n",
        "**The following steps are required, regardless of your notebook environment.**\n",
        "\n",
        "This tutorial is designed to use training data that is in a public Cloud Storage bucket and a local Cloud Storage bucket for your batch predictions. You may alternatively use your own training data that you have stored in a local Cloud Storage bucket.\n",
        "\n",
        "Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bucket"
      },
      "outputs": [],
      "source": [
        "BUCKET_NAME = \"[your-bucket-name]\"  # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "autoset_bucket"
      },
      "outputs": [],
      "source": [
        "if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
        "    BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yU6VuylGJjkM"
      },
      "source": [
        "**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QVRixM4oJjkM"
      },
      "outputs": [],
      "source": [
        "! gsutil mb -l $REGION gs://$BUCKET_NAME"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Mi-Pdh6QJjkN"
      },
      "source": [
        "Finally, validate access to your Cloud Storage bucket by examining its contents:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "aqDptKpYJjkN"
      },
      "outputs": [],
      "source": [
        "! gsutil ls -al gs://$BUCKET_NAME"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fR9geV9pJjkO"
      },
      "source": [
        "### Set up variables\n",
        "\n",
        "Next, set up some variables used throughout the tutorial.\n",
        "### Import libraries and define constants"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hIHTX-pkJjkO"
      },
      "source": [
        "#### Import AutoML SDK\n",
        "\n",
        "Import the AutoM SDK into our Python environment."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qrSkIKVgJjkO"
      },
      "outputs": [],
      "source": [
        "import json\n",
        "import os\n",
        "import sys\n",
        "import time\n",
        "\n",
        "from google.cloud import automl\n",
        "from google.protobuf.json_format import MessageToJson\n",
        "from google.protobuf.struct_pb2 import Value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lbv411XjJjkP"
      },
      "source": [
        "#### AutoML constants\n",
        "\n",
        "Setup up the following constants for AutoML:\n",
        "\n",
        "- `PARENT`: The AutoM location root path for dataset, model and endpoint resources."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Gx8Eos8PJjkP"
      },
      "outputs": [],
      "source": [
        "# AutoM location root path for your dataset, model and endpoint resources\n",
        "PARENT = \"projects/\" + PROJECT_ID + \"/locations/\" + REGION"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hBaOc_HRc4zC"
      },
      "source": [
        "## Clients\n",
        "\n",
        "The AutoML SDK works as a client/server model. On your side (the Python script) you will create a client that sends requests and receives responses from the server (AutoML).\n",
        "\n",
        "You will use several clients in this tutorial, so set them all up upfront."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5t0zkxqPc4zC"
      },
      "outputs": [],
      "source": [
        "def automl_client():\n",
        "    return automl.AutoMlClient()\n",
        "\n",
        "\n",
        "def perdictions_client():\n",
        "    return automl.PredictionServiceClient()\n",
        "\n",
        "\n",
        "def operations_client():\n",
        "    return automl.AutoMlClient()._transport.operations_client\n",
        "\n",
        "\n",
        "clients = {}\n",
        "clients[\"automl\"] = automl_client()\n",
        "clients[\"predictions\"] = perdictions_client()\n",
        "clients[\"operations\"] = operations_client()\n",
        "\n",
        "for client in clients.items():\n",
        "    print(client)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "import_file:flowers,csv,icn"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "IMPORT_FILE = \"gs://cloud-samples-data/language/claritin.csv\"\n",
        "with tf.io.gfile.GFile(IMPORT_FILE, \"r\") as f:\n",
        "    content = f.readlines()\n",
        "\n",
        "IMPORT_FILE = \"gs://\" + BUCKET_NAME + \"/claritin.csv\"\n",
        "with tf.io.gfile.GFile(IMPORT_FILE, \"w\") as f:\n",
        "    for line in content:\n",
        "        f.write(\",\".join(line.split(\",\")[0:-1]) + \"\\n\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ed725169cbfa"
      },
      "outputs": [],
      "source": [
        "! gsutil cat $IMPORT_FILE | head -n 10"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "d3a0464dd6f3"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "@freewrytin God is way too good for Claritin,2\n",
        "I need Claritin. So bad. When did I become cursed with allergies?,3\n",
        "Thank god for Claritin.,4\n",
        "\"And what's worse is that I reached my 3-day limit on the nose spray yesterday, which means I have to rely on Claritin.\",2\n",
        "Time to take some Claritin or Allegra or something. I need my voice,3\n",
        "Oh my RT @imsydneycharles: I just want it to be on record somewhere that I took Claritin and Benadryl together...just in case I pass out,2\n",
        "Bouta take a Claritin _ÛªÛ_Ûª_ÛªÌâ FML !!,3\n",
        "Commander Loratadine Generic A Sarcelles: Commander Loratadine Generic A Sarcelles Claritin =Ûª_Ûª__ http://t.co/mOleL8AM,2\n",
        "\"Zyrtec, Claritin, Suddafed, Nasal Spray.. I feel like a drug addict taking these Allergy medicine. Please Allergy season.. DISAPPEAR!!\",1\n",
        "\"Ûª_Ûª_ÛªÕ@SheLovesThatD: If she has allergies, give her the Claritin D.Ûª_Ûª_Ì_å @Sweeno_thakid41 @B_Original16 @luke_CYwalker14\",3\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_create_dataset:migration"
      },
      "source": [
        "## Create a dataset"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_datasets_create:migration,old"
      },
      "source": [
        "### [projects.locations.datasets.create](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.datasets/create)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ozaMOwdNJjkT"
      },
      "source": [
        "#### Request"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OMZr1t8mc4zF"
      },
      "outputs": [],
      "source": [
        "dataset = {\n",
        "    \"display_name\": \"claritin_\" + TIMESTAMP,\n",
        "    \"text_sentiment_dataset_metadata\": {\"sentiment_max\": 4},\n",
        "}\n",
        "\n",
        "\n",
        "print(\n",
        "    MessageToJson(\n",
        "        automl.CreateDatasetRequest(parent=PARENT, dataset=dataset).__dict__[\"_pb\"]\n",
        "    )\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "f1354773b89e"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"parent\": \"projects/migration-ucaip-training/locations/us-central1\",\n",
        "  \"dataset\": {\n",
        "    \"displayName\": \"claritin_20210304132912\",\n",
        "    \"textSentimentDatasetMetadata\": {\n",
        "      \"sentimentMax\": 4\n",
        "    }\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vHkuSZxkJjkT"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ifCavN75c4zG"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].create_dataset(parent=PARENT, dataset=dataset)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FLqkZMD2JjkT"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "87xXwkaVc4zG"
      },
      "outputs": [],
      "source": [
        "result = request.result()\n",
        "\n",
        "print(MessageToJson(result.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "f9c171ffceee"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/datasets/TST1994716952680988672\"\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "86e845326428"
      },
      "outputs": [],
      "source": [
        "# The full unique ID for the dataset\n",
        "dataset_id = result.name\n",
        "# The short numeric ID for the dataset\n",
        "dataset_short_id = dataset_id.split(\"/\")[-1]\n",
        "\n",
        "print(dataset_id)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_datasets_importdata:migration,old"
      },
      "source": [
        "### [projects.locations.datasets.importData](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.datasets/importData)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "y3T7l1xAc4zH"
      },
      "source": [
        "#### Request"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "jjOtdiJBc4zH"
      },
      "outputs": [],
      "source": [
        "input_config = {\"gcs_source\": {\"input_uris\": [IMPORT_FILE]}}\n",
        "\n",
        "print(\n",
        "    MessageToJson(\n",
        "        automl.ImportDataRequest(name=dataset_id, input_config=input_config).__dict__[\n",
        "            \"_pb\"\n",
        "        ]\n",
        "    )\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c0fd2b918d71"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/datasets/TST1994716952680988672\",\n",
        "  \"inputConfig\": {\n",
        "    \"gcsSource\": {\n",
        "      \"inputUris\": [\n",
        "        \"gs://migration-ucaip-trainingaip-20210304132912/claritin.csv\"\n",
        "      ]\n",
        "    }\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7QSqElzPc4zH"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "FWm339Twc4zI"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].import_data(name=dataset_id, input_config=input_config)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AQVw0rQsc4zI"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Fur_ANcMc4zI"
      },
      "outputs": [],
      "source": [
        "result = request.result()\n",
        "\n",
        "print(MessageToJson(result))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NT2ras8rhC_D"
      },
      "source": [
        "*Example output*:\r\n",
        "```\r\n",
        "{}\r\n",
        "```"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_create_and_deploy_model:migration"
      },
      "source": [
        "## Train a model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_models_create:migration,old"
      },
      "source": [
        "### [projects.locations.models.create](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models/create)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bfPBtMYZc4zJ"
      },
      "source": [
        "#### Request"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LbWL7tq2c4zJ"
      },
      "outputs": [],
      "source": [
        "model = {\n",
        "    \"display_name\": \"claritin_\" + TIMESTAMP,\n",
        "    \"dataset_id\": dataset_short_id,\n",
        "    \"text_sentiment_model_metadata\": {},\n",
        "}\n",
        "\n",
        "print(\n",
        "    MessageToJson(automl.CreateModelRequest(parent=PARENT, model=model).__dict__[\"_pb\"])\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "e080c4730d86"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"parent\": \"projects/migration-ucaip-training/locations/us-central1\",\n",
        "  \"model\": {\n",
        "    \"displayName\": \"claritin_20210304132912\",\n",
        "    \"datasetId\": \"TST1994716952680988672\",\n",
        "    \"textSentimentModelMetadata\": {}\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "duJfz52Rc4zJ"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kHS2pwcec4zK"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].create_model(parent=PARENT, model=model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vmkA2d5-c4zK"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "o8P7a8nQc4zK"
      },
      "outputs": [],
      "source": [
        "result = request.result()\n",
        "\n",
        "print(MessageToJson(result.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "498f8fd21640"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272\"\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "865fcdfa7262"
      },
      "outputs": [],
      "source": [
        "# The full unique ID for the training pipeline\n",
        "model_id = result.name\n",
        "# The short numeric ID for the training pipeline\n",
        "model_short_id = model_id.split(\"/\")[-1]\n",
        "\n",
        "print(model_short_id)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_evaluate_the_model:migration"
      },
      "source": [
        "## Evaluate the model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yfufMwAEJjkX"
      },
      "source": [
        "### [projects.locations.models.modelEvaluations.list](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models.modelEvaluations/list)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ro1Hr7Hbc4zL"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hBEu4Ghoc4zL"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].list_model_evaluations(parent=model_id, filter=\"\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "X9XycwTYc4zM"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ZbX6Z00uc4zM"
      },
      "outputs": [],
      "source": [
        "model_evaluations = [json.loads(MessageToJson(me.__dict__[\"_pb\"])) for me in request]\n",
        "# The evaluation slice\n",
        "evaluation_slice = request.model_evaluation[0].name\n",
        "\n",
        "print(json.dumps(model_evaluations, indent=2))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6835b78da85b"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "[\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/54870628009945864\",\n",
        "    \"annotationSpecId\": \"8301667931964571648\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.33333334,\n",
        "      \"recall\": 0.16666667,\n",
        "      \"f1Score\": 0.22222222\n",
        "    },\n",
        "    \"displayName\": \"4\"\n",
        "  },\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/1597159550285093673\",\n",
        "    \"annotationSpecId\": \"1384138904323489792\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.5,\n",
        "      \"recall\": 0.296875,\n",
        "      \"f1Score\": 0.37254903\n",
        "    },\n",
        "    \"displayName\": \"1\"\n",
        "  },\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/3521790980763365687\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"evaluatedExampleCount\": 452,\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.6238938,\n",
        "      \"recall\": 0.6238938,\n",
        "      \"f1Score\": 0.6238938,\n",
        "      \"meanAbsoluteError\": 0.47566372,\n",
        "      \"meanSquaredError\": 0.69690263,\n",
        "      \"linearKappa\": 0.41007927,\n",
        "      \"quadraticKappa\": 0.45938763,\n",
        "      \"confusionMatrix\": {\n",
        "        \"annotationSpecId\": [\n",
        "          \"7148746427357724672\",\n",
        "          \"1384138904323489792\",\n",
        "          \"5995824922750877696\",\n",
        "          \"3689981913537183744\",\n",
        "          \"8301667931964571648\"\n",
        "        ],\n",
        "        \"row\": [\n",
        "          {\n",
        "            \"exampleCount\": [\n",
        "              2,\n",
        "              4,\n",
        "              1,\n",
        "              1,\n",
        "              1\n",
        "            ]\n",
        "          },\n",
        "          {\n",
        "            \"exampleCount\": [\n",
        "              3,\n",
        "              19,\n",
        "              14,\n",
        "              28,\n",
        "              0\n",
        "            ]\n",
        "          },\n",
        "          {\n",
        "            \"exampleCount\": [\n",
        "              0,\n",
        "              7,\n",
        "              67,\n",
        "              63,\n",
        "              1\n",
        "            ]\n",
        "          },\n",
        "          {\n",
        "            \"exampleCount\": [\n",
        "              1,\n",
        "              8,\n",
        "              19,\n",
        "              191,\n",
        "              4\n",
        "            ]\n",
        "          },\n",
        "          {\n",
        "            \"exampleCount\": [\n",
        "              0,\n",
        "              0,\n",
        "              0,\n",
        "              15,\n",
        "              3\n",
        "            ]\n",
        "          }\n",
        "        ],\n",
        "        \"displayName\": [\n",
        "          \"0\",\n",
        "          \"1\",\n",
        "          \"2\",\n",
        "          \"3\",\n",
        "          \"4\"\n",
        "        ]\n",
        "      }\n",
        "    }\n",
        "  },\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/3727703410992997127\",\n",
        "    \"annotationSpecId\": \"3689981913537183744\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.6409396,\n",
        "      \"recall\": 0.85650223,\n",
        "      \"f1Score\": 0.7332054\n",
        "    },\n",
        "    \"displayName\": \"3\"\n",
        "  },\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/4692810493650008310\",\n",
        "    \"annotationSpecId\": \"7148746427357724672\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.33333334,\n",
        "      \"recall\": 0.22222222,\n",
        "      \"f1Score\": 0.26666668\n",
        "    },\n",
        "    \"displayName\": \"0\"\n",
        "  },\n",
        "  {\n",
        "    \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/8390011688796741170\",\n",
        "    \"annotationSpecId\": \"5995824922750877696\",\n",
        "    \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "    \"textSentimentEvaluationMetrics\": {\n",
        "      \"precision\": 0.6633663,\n",
        "      \"recall\": 0.48550725,\n",
        "      \"f1Score\": 0.5606694\n",
        "    },\n",
        "    \"displayName\": \"2\"\n",
        "  }\n",
        "]\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "i6T0bzuNJjkY"
      },
      "source": [
        "### [projects.locations.models.modelEvaluations.get](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models.modelEvaluations/get)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cu1lRCSOc4zM"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XIIHbDYWc4zM"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].get_model_evaluation(name=evaluation_slice)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aMgYNNWgc4zN"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hSWpi4v1c4zN"
      },
      "outputs": [],
      "source": [
        "print(MessageToJson(request.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "b11081c8c71e"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272/modelEvaluations/54870628009945864\",\n",
        "  \"annotationSpecId\": \"8301667931964571648\",\n",
        "  \"createTime\": \"2021-03-04T17:15:51.851420Z\",\n",
        "  \"textSentimentEvaluationMetrics\": {\n",
        "    \"precision\": 0.33333334,\n",
        "    \"recall\": 0.16666667,\n",
        "    \"f1Score\": 0.22222222\n",
        "  },\n",
        "  \"displayName\": \"4\"\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iRUvxxAKc4zN"
      },
      "source": [
        "## Make batch predictions"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "caA77NMbc4zN"
      },
      "source": [
        "### Make the batch input file\r\n",
        "\r\n",
        "To request a batch of predictions from AutoML Video, create a CSV file that lists the Cloud Storage paths to the videos that you want to annotate. You can also specify a start and end time to tell AutoML Video to only annotate a segment (segment-level) of the video. The start time must be zero or greater and must be before the end time. The end time must be greater than the start time and less than or equal to the duration of the video. You can also use inf to indicate the end of a video."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5beaf1d4077c"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "gcs_input_uri = \"gs://\" + BUCKET_NAME + \"/test.csv\"\n",
        "with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n",
        "    item_1 = \"gs://cloud-samples-data/language/sentiment-positive.txt\"\n",
        "    ! gsutil cp $item_1 gs://$BUCKET_NAME\n",
        "    f.write(\"gs://\" + BUCKET_NAME + \"/sentiment-positive.txt\" + \"\\n\")\n",
        "\n",
        "    item_2 = \"gs://cloud-samples-data/language/sentiment-negative.txt\"\n",
        "    ! gsutil cp $item_2 gs://$BUCKET_NAME\n",
        "    f.write(\"gs://\" + BUCKET_NAME + \"/sentiment-negative.txt\")\n",
        "\n",
        "! gsutil cat $gcs_input_uri"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "031c110f4c79"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "gs://migration-ucaip-trainingaip-20210304132912/sentiment-positive.txt\n",
        "gs://migration-ucaip-trainingaip-20210304132912/sentiment-negative.txt\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_models_batchpredict:migration,old"
      },
      "source": [
        "### [projects.locations.models.batchPredict](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models/batchPredict)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UNxo4fR5c4zO"
      },
      "source": [
        "#### Request"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Vu-8O5Y2c4zO"
      },
      "outputs": [],
      "source": [
        "input_config = {\"gcs_source\": {\"input_uris\": [gcs_input_uri]}}\n",
        "\n",
        "output_config = {\n",
        "    \"gcs_destination\": {\"output_uri_prefix\": \"gs://\" + f\"{BUCKET_NAME}/batch_output/\"}\n",
        "}\n",
        "\n",
        "print(\n",
        "    MessageToJson(\n",
        "        automl.BatchPredictRequest(\n",
        "            name=model_id, input_config=input_config, output_config=output_config\n",
        "        ).__dict__[\"_pb\"]\n",
        "    )\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9defd1c4d1a0"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272\",\n",
        "  \"inputConfig\": {\n",
        "    \"gcsSource\": {\n",
        "      \"inputUris\": [\n",
        "        \"gs://migration-ucaip-trainingaip-20210304132912/test.csv\"\n",
        "      ]\n",
        "    }\n",
        "  },\n",
        "  \"outputConfig\": {\n",
        "    \"gcsDestination\": {\n",
        "      \"outputUriPrefix\": \"gs://migration-ucaip-trainingaip-20210304132912/batch_output/\"\n",
        "    }\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kc_59mp8c4zP"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TSk7yRiqc4zP"
      },
      "outputs": [],
      "source": [
        "request = clients[\"predictions\"].batch_predict(\n",
        "    name=model_id, input_config=input_config, output_config=output_config\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mcqKVj2Xc4zP"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bSTEmLgGc4zP"
      },
      "outputs": [],
      "source": [
        "result = request.result()\n",
        "\n",
        "print(MessageToJson(result.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c4CvqTBhtMNi"
      },
      "source": [
        "*Example output*:\r\n",
        "```\r\n",
        "{}\r\n",
        "```"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FQjooSKyc4zQ"
      },
      "source": [
        "## Make online predictions\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nrRQoOCVc4zQ"
      },
      "source": [
        "#### Prepare data item for online prediction\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mMdW9VbJc4zQ"
      },
      "outputs": [],
      "source": [
        "test_data = ! gsutil cat $IMPORT_FILE | head -n1\n",
        "\n",
        "test_item = str(test_data[0]).split(\",\")[0]\n",
        "test_label = str(test_data[0]).split(\",\")[1]\n",
        "\n",
        "print((test_item, test_label))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8981b397c864"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "('@freewrytin God is way too good for Claritin', '2')\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_models_deploy:migration,old"
      },
      "source": [
        "### [projects.locations.models.deploy](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models/deploy)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "h0AxsMSjc4zQ"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_Uxf6m8Cc4zR"
      },
      "outputs": [],
      "source": [
        "request = clients[\"automl\"].deploy_model(name=model_id)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Rj8jg_GVc4zR"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "q7rox1YCc4zR"
      },
      "outputs": [],
      "source": [
        "result = request.result()\n",
        "\n",
        "print(MessageToJson(result))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "s1ikNfXniG-3"
      },
      "source": [
        "*Example output*:\r\n",
        "```\r\n",
        "{}\r\n",
        "```"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "text_models_predict:migration,old"
      },
      "source": [
        "### [projects.locations.models.predict](https://cloud.google.com/automl/docs/reference/rest/v1beta1/projects.locations.models/predict)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "g688Vva3c4zS"
      },
      "outputs": [],
      "source": [
        "payload = {\"text_snippet\": {\"content\": test_item, \"mime_type\": \"text/plain\"}}\n",
        "\n",
        "prediction_request = automl.PredictRequest(\n",
        "    name=model_id,\n",
        "    payload=payload,\n",
        ")\n",
        "\n",
        "print(MessageToJson(prediction_request.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cd025e2420db"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"name\": \"projects/116273516712/locations/us-central1/models/TST4078882474816438272\",\n",
        "  \"payload\": {\n",
        "    \"textSnippet\": {\n",
        "      \"content\": \"@freewrytin God is way too good for Claritin\",\n",
        "      \"mimeType\": \"text/plain\"\n",
        "    }\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kgznWoQcc4zS"
      },
      "source": [
        "#### Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GUOkxQwic4zS"
      },
      "outputs": [],
      "source": [
        "request = clients[\"predictions\"].predict(request=prediction_request)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9MfWbNzhc4zS"
      },
      "source": [
        "#### Response"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "xZZliEjoc4zS"
      },
      "outputs": [],
      "source": [
        "print(MessageToJson(request.__dict__[\"_pb\"]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dc047af97666"
      },
      "source": [
        "*Example output*:\n",
        "```\n",
        "{\n",
        "  \"payload\": [\n",
        "    {\n",
        "      \"textSentiment\": {\n",
        "        \"sentiment\": 3\n",
        "      }\n",
        "    }\n",
        "  ],\n",
        "  \"metadata\": {\n",
        "    \"sentiment_score\": \"0.30955505\"\n",
        "  }\n",
        "}\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bQ-VVaSxJjkd"
      },
      "source": [
        "# Cleaning up\r\n",
        "\r\n",
        "To clean up all GCP resources used in this project, you can [delete the GCP\r\n",
        "project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\r\n",
        "\r\n",
        "Otherwise, you can delete the individual resources you created in this tutorial."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "oNJs94pKc4zT"
      },
      "outputs": [],
      "source": [
        "delete_dataset = True\n",
        "delete_model = True\n",
        "delete_bucket = True\n",
        "\n",
        "# Delete the dataset using the AutoML fully qualified identifier for the dataset\n",
        "try:\n",
        "    if delete_dataset:\n",
        "        clients[\"automl\"].delete_dataset(name=dataset_id)\n",
        "except Exception as e:\n",
        "    print(e)\n",
        "\n",
        "# Delete the model using the AutoML fully qualified identifier for the model\n",
        "try:\n",
        "    if delete_model:\n",
        "        clients[\"automl\"].delete_model(name=model_id)\n",
        "except Exception as e:\n",
        "    print(e)\n",
        "\n",
        "if delete_bucket and \"BUCKET_NAME\" in globals():\n",
        "    ! gsutil rm -r gs://$BUCKET_NAME"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "UJ8 legacy AutoML Natural Language - Text Sentiment Analysis.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
